/**
 * Copyright (c), 2012~2020 iot.10086.cn All Rights Reserved
 *
 * @file main.c
 * @brief
 */

/*****************************************************************************/
/* Includes                                                                  */
/*****************************************************************************/
#include <stdio.h>
#include <string.h>

#include "common.h"
#include "data_types.h"
#include "plat_time.h"
#include "tm_api.h"
#include "tm_data.h"
#include "tm_user.h"

///** OneNET平台产品ID*/
//#define TM_PRODUCT_ID ""

///** 设备唯一识别码，产品内唯一*/
//#define TM_DEVICE_NAME ""

///** 认证信息--设备key*/
//#define TM_ACCESS_KEY ""

///** token有效时间，默认设置为2030年12月*/
//#define TM_EXPIRE_TIME 1924833600

int tm_main(int arg, char* argv[])
{
    struct tm_downlink_tbl_t dl_tbl;
    int                      ret = 0;

    dl_tbl.prop_tbl      = tm_prop_list;
    dl_tbl.prop_tbl_size = tm_prop_list_size;
    dl_tbl.svc_tbl       = tm_svc_list;
    dl_tbl.svc_tbl_size  = tm_svc_list_size;

    /** 设备初始化*/
    ret = tm_init(&dl_tbl);
    CHECK_EXPR_GOTO(ERR_OK != ret, _END, "ThingModel init failed!\n");
    logi("ThingModel init ok");

    /** 设备登录*/
    ret = tm_login(TM_PRODUCT_ID, TM_DEVICE_NAME, TM_ACCESS_KEY, TM_EXPIRE_TIME, 3000);
    CHECK_EXPR_GOTO(ERR_OK != ret, _END, "ThingModel login failed!");
    logi("ThingModel login ok");

    while (1) {
        if (0 != (ret = tm_step(200))) {
            logi("ThingModel tm_step failed,ret is %d",ret);
            break;
        }
        time_delay_ms(50);
    }

    /** 设备注销*/
    tm_logout(3000);
_END:
    return 0;
}
